package com.cl.juc;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.FutureTask;

public class TestFutureTask {

    public static void main(String[] args) throws ExecutionException, InterruptedException {
        /*
        * FutureTask , 构造传入线程的Callable接口(一个线程中带返回值的接口) ,
        * 再将此 FutureTask 对象传入 Thread 的构造函数中,并且启动此线程,当调用 FutureTask 对象的get方法时,
        * 如果此callable线程还在运行中,则调用get方法的线程会阻塞,直到 callable 线程运行完,将线程运行的结果返回
        * */
        FutureTask<Integer> integerFutureTask = new FutureTask<>(new Callable<Integer>() {
            @Override
            public Integer call() throws Exception {
                Thread.sleep(4000);
                return 100;
            }
        });

        new Thread(integerFutureTask).start();

        System.out.println("main......");
        Integer integer = integerFutureTask.get();
        System.out.println(integer);
    }

}
